<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="头像" prop="avatar">
				<sc-upload
					v-if="mode!='show'"
					v-model="form.avatar"
					:apiObj="apiObj"
					title="上传头像"
				></sc-upload>
				<el-image v-if="mode=='show'" style="width:150px;height: 150px;" :src="form.avatar"></el-image>
			</el-form-item>
			<el-form-item label="排序号" prop="sortNo">
				<el-input-number v-model="form.sortNo" clearable></el-input-number>
			</el-form-item>
			<el-form-item label="登录账号" prop="accountId">
				<el-input v-model="form.accountId" placeholder="用于登录系统" clearable v-if="mode=='add'"></el-input>
				<span v-if="mode!='add'">{{form.accountId}}</span>
			</el-form-item>
			<el-form-item label="登录别名" prop="alias">
				<el-input v-model="form.alias" placeholder="用于登录系统" clearable></el-input>
				<div class="el-form-item-msg">可用于系统登陆用</div>
			</el-form-item>
			<el-form-item label="身份证号" prop="idCard">
				<el-input v-model="form.idCard" placeholder="请输入身份证号" clearable></el-input>
			</el-form-item>
			<el-form-item label="姓名" prop="userName">
				<el-input v-model="form.userName" placeholder="请输入完整的真实姓名" clearable></el-input>
			</el-form-item>
			<el-form-item label="联系电话" prop="communicatePhone">
				<el-input v-model="form.communicatePhone" placeholder="请输入联系电话" clearable></el-input>
			</el-form-item>
			<el-form-item label="职务" prop="workPost">
				<el-input v-model="form.workPost" placeholder="请输入职务" clearable></el-input>
			</el-form-item>
			<el-form-item label="性别" prop="sex">
				<el-select v-model="form.sex" placeholder="请选择" style="width: 200px">
					<el-option label="男" value="1"></el-option>
					<el-option label="女" value="0"></el-option>
				</el-select>
			</el-form-item>
			<template v-if="mode=='add'">
				<el-form-item label="登录密码" prop="passWord">
					<el-input type="password" v-model="form.passWord" clearable show-password></el-input>
				</el-form-item>
				<el-form-item label="确认密码" prop="passWord2">
					<el-input type="password" v-model="form.passWord2" clearable show-password></el-input>
				</el-form-item>
			</template>
			<el-form-item label="涉密等级" prop="classifiedLevel">
				<el-select v-model="form.classifiedLevel">
					<el-option v-for="item in options1" :key="item.value" :label="item.label" :value="item.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="验证方式" prop="passwordType">
				<el-select v-model="form.passwordType">
					<el-option v-for="item in options2" :key="item.value" :label="item.label" :value="item.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="人员来源">
				<el-select v-model="form.isIn">
					<el-option v-for="item in options3" :key="item.value" :label="item.label" :value="item.value">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="考勤规则">
				<el-select v-model="form.attend" filterable style="width: 100%" placeholder="考勤规则">
					<el-option v-for="item in attend" :key="item.configId" :label="item.title" :value="item.configId"/>
				</el-select>
			</el-form-item>
			<el-form-item label="所属部门" prop="deptId">
				<el-cascader v-model="form.deptId" :options="depts" :props="deptsProps" clearable :show-all-levels="false" style="width: 100%;"></el-cascader>
			</el-form-item>
			<el-form-item label="行政职务" prop="userLevel">
				<sc-select v-model="form.userLevel" :apiObj="$API.system.userLevel.getUserLevelForSelect" clearable filterable style="width: 100%" placeholder="行政职务"></sc-select>
			</el-form-item>
			<el-form-item label="用户权限" prop="roleId">
				<el-select v-model="form.roleId" multiple filterable style="width: 100%">
					<el-option v-for="item in userRole" :key="item.roleId" :label="item.label" :value="item.roleId"/>
				</el-select>
			</el-form-item>
			<el-form-item label="上级领导">
				<select-user-dialog v-model:accountId="form.leadId" :mode="false"/>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
import selectUserDialog from "@/views/module/selectUserDialog";
import msgBoxCheckBox from "@/views/module/msgBoxCheckBox";
	export default {
		emits: ['success', 'closed'],
		components: {
			selectUserDialog
		},
		data() {
			return {
				apiObj:this.$API.file.uploadAvatar,
				options1:[
					{value:"1",label:'普通'},
					{value:"2",label:'秘密'},
					{value:"3",label:'机密'},
					{value:"4",label:'绝密'}
				],
				options2:[
					{value:"1",label:'密码校验'},
					{value:"2",label:'U-KEY'},
					{value:"3",label:'U-KEY+密码'}
				],
				options3:[
					{value:"0",label:'内部'},
					{value:"1",label:'外部'}
				],
				mode: "add",
				titleMap: {
					add: '新增用户',
					edit: '编辑用户',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					staffId:"",
					accountId:"",
					sortNo:0,
					alias:"",
					userName: "",
					isIn:"0",
					avatar: "",
					name: "",
					sex:'1',
					deptId: "",
					leadId:"",
					userLevel:"",
					passWord:'',
					passWord2:'',
					classifiedLevel:"1",
					passwordType:"1",
					attend:'',
					portalId:'0',
					idCard:"",
					roleId: []
				},
				//验证规则
				rules: {
					accountId: [
						{required: true, message: '请输入登录账号'}
					],
					userName: [
						{required: true, message: '请输入真实姓名'}
					],
					idCard: [
						{required: true, message: '请输入身份证号'}
					],
					sex: [
						{required: true, message: '请选择性别'}
					],
					communicatePhone: [
						{required: true, message: '请输入手机号码'},
						{validator: (rule, value, callback) => {
							if (!/^1[3-9]\d{9}$/.test(this.form.communicatePhone)) {
								callback(new Error('手机号格式错误'));
							}
							callback();
						}}
					],
					workPost: [
						{required: true, message: '请输入职务'}
					],
					passWord: [
						{required: true, message: '请输入登录密码'},
						{validator: (rule, value, callback) => {
							if (this.form.passWord2 !== '') {
								this.$refs.dialogForm.validateField('passWord2');
							}
							callback();
						}}
					],
					passWord2: [
						{required: true, message: '请再次输入密码'},
						{validator: (rule, value, callback) => {
							if (value !== this.form.passWord) {
								callback(new Error('两次输入密码不一致!'));
							}else{
								callback();
							}
						}}
					],
					
					userLevel: [
						{required: true, message: '请选择用户行政职务'}
					],
					deptId: [
						{required: true, message: '请选择所属部门'}
					],
					roleId: [
						{required: true, message: '请选择所属角色', trigger: 'change'}
					],
					// classifiedLevel:[
					// 	{required: true, message: '涉密等级不能为空', trigger: 'change'}
					// ],
					// passwordType:[
					// 	{required: true, message: '验证方式不能为空', trigger: 'change'}
					// ]
				},
				//所需数据选项
				userRole: [],
				groupsProps: {
					value: "id",
					multiple: true,
					checkStrictly: true
				},
				depts: [],
				deptsProps: {
					label:"deptName",
					value: "deptId",
					emitPath: false,
					checkStrictly: true
				},
				attend:[],
			}
		},
		mounted() {
			this.getUserRole();
			this.getDept();
			this.getAttendConfig();
		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//加载树数据
			async getUserRole(){
				var res = await this.$API.system.userRole.getAllUserRoleForSelect.get();
				this.userRole = res.data;
			},
			async getDept(){
				var res = await this.$API.system.unitDept.list.get();
				this.depts = res.data;
			},
			async getAttendConfig(){
				var res = await this.$API.system.attendConfig.getAllAttendConfigList.get();
				this.attend = res.data;
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						if(this.form.roleId.length>0)
						{
							this.form.roleId = this.form.roleId.join(",");
						}else
						{
							this.form.roleId = "";
						}
						var res;
						if(this.mode=="add")
						{
							res = await this.$API.system.user.insert.post(this.form);
						}else if(this.mode=="edit")
						{
							res = await this.$API.system.user.updateUserInfo.post(this.form);
						}
						this.isSaveing = false;
						if(res.code == 200){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success(res.message)
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
						this.form.roleId = this.form.roleId.split(",")
					}else{
						return false;
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form.sortNo = data.sortNo;
				this.form.accountId = data.accountId;
				this.form.staffId = data.staffId;
				this.form.userName = data.userName;
				this.form.alias = data.alias
				this.form.sex = data.sex;
				this.form.dept = data.deptId;
				this.form.avatar = data.avatar;
				this.form.roleId = data.roleId.split(",");
				this.form.deptId = data.deptId;
				this.form.isIn = data.isIn;
				this.form.userLevel=data.userLevel;
				this.form.leadId = data.leadId;
				this.form.classifiedLevel = data.classifiedLevel;
				this.form.passwordType = data.passwordType;
				this.form.attend = data.attend
				this.form.idCard = data.idCard;
				this.form.workPost = data.workPost;
				this.form.communicatePhone  = data.communicatePhone;
				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				//Object.assign(this.form, data)
			},
		}
	}
</script>

<style>
</style>
